using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using System.Drawing;
using BbqFramework.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using BbqFramework.Drawing.Imaging;

namespace Bbq.Gallery
{
    /// <summary>
    /// 
    /// </summary>
    class GalleryModule : IHttpModule
    {
		private HttpApplication _context = null;

        #region IHttpModule Members

        /// <summary>
        /// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"></see>.
        /// </summary>
        public void Dispose()
        {
			if (_context != null)
			{
				_context.BeginRequest -= context_BeginRequest;
				_context.Dispose();
				_context = null;
			}
        }

        /// <summary>
        /// Initializes a module and prepares it to handle requests.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpApplication"></see> that provides access to the methods, properties, and events common to all application objects within an ASP.NET application</param>
        public void Init(HttpApplication context)
        {
			context.BeginRequest += context_BeginRequest;
        }

        /// <summary>
        /// Handles the BeginRequest event of the context control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            string url = app.Request.Url.AbsolutePath;
			if (url.EndsWith(GalleryHelper.ThumbnailHandlerName, StringComparison.OrdinalIgnoreCase))
			{

				app.Response.End();
				app.CompleteRequest();
			}
        }

        /// <summary>
        /// Sets the response for thumbnail.
        /// </summary>
        /// <param name="response">The response.</param>
        private void SetResponseForThumbnail(HttpResponse response)
        {
            response.BufferOutput = true;
            response.ContentType = "image/jpeg";
            response.Cache.SetExpires(DateTime.Now.AddMinutes(15));
            response.Cache.SetCacheability(HttpCacheability.Public);
            response.Cache.SetSlidingExpiration(true);
        }

        #endregion

    }

}
